package com.zjdiepu.www.task.trailer;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.zjdiepu.www.base.enums.status.DpStatus;
import com.zjdiepu.www.base.enums.type.TypeData;
import com.zjdiepu.www.base.enums.type.TypeTrailer;
import com.zjdiepu.www.common.util.UUIDFactory;
import com.zjdiepu.www.ibiz.dh.IDhOverdueCollectionPhoneService;
import com.zjdiepu.www.ibiz.dh.IDhOverdueLetterLawyersService;
import com.zjdiepu.www.ibiz.dh.IDhOverdueTrailerService;
import com.zjdiepu.www.ibiz.dz.IDzMortgageService;
import com.zjdiepu.www.ibiz.system.IBaseBusiUnitService;
import com.zjdiepu.www.ibiz.system.IBaseSalersmanService;
import com.zjdiepu.www.ibiz.system.IBaseSysParamsService;
import com.zjdiepu.www.ibiz.yw.IYwBusinessLoanService;
import com.zjdiepu.www.ibiz.yw.IYwBusinessService;
import com.zjdiepu.www.model.dh.dto.DhOverdueCollectionPhone;
import com.zjdiepu.www.model.dh.dto.DhOverdueLetterLawyers;
import com.zjdiepu.www.model.dh.dto.DhOverdueTrailer;
import com.zjdiepu.www.model.dz.dto.DzMortgage;
import com.zjdiepu.www.model.dz.ext.DzMortgageExt;
import com.zjdiepu.www.model.system.dto.BaseBusiUnit;
import com.zjdiepu.www.model.system.dto.BaseSalersman;
import com.zjdiepu.www.model.system.dto.BaseSysParams;
import com.zjdiepu.www.model.yw.dto.YwBusiness;
import com.zjdiepu.www.model.yw.dto.YwBusinessLoan;
import com.zjdiepu.www.model.yw.ext.YwBusinessLoanExt;

@Component
public class TrailerListTask {

	private Logger logger = LoggerFactory.getLogger(getClass());
	
	@Resource
	private IBaseSysParamsService baseSysParamsService;
	@Resource
	private IDzMortgageService dzmortgageService;
	@Resource
	private IDhOverdueTrailerService dhOverdueTrailerService;
    @Resource
    private IYwBusinessService ywBusinessService;
    @Resource
    private IBaseSalersmanService baseSalersmanService;
    @Resource
    private IBaseBusiUnitService baseBusiUnitService;
    @Resource
    private IDhOverdueLetterLawyersService dhOverdueLetterLawyersService;
    @Resource
    private IDhOverdueCollectionPhoneService dhOverdueCollectionPhoneService;
    @Resource
    private IYwBusinessLoanService ywBusinessLoanService;
    
    @Scheduled(cron="${schedule.tclb.alert.cron}")
    public void getTrailerList(){
    	yuqiList();
    	diYaList();
    }
    
	
	/**
	 * 逾期
	 */
	private void yuqiList() {
		logger.debug("yuqiList task start.");
		BaseSysParams sys1= new BaseSysParams();
		sys1.setCodeParam("TowCar_PhoneCollectionNum");
		sys1=baseSysParamsService.selectRefined(sys1);
		
		BaseSysParams sys2=new  BaseSysParams();
		sys2.setCodeParam("TowCar_Letter_LawyersNum");
		sys2=baseSysParamsService.selectRefined(sys2);
		
		BaseSysParams sys3=new  BaseSysParams();
		sys3.setCodeParam("TowCar_PayAmountMonthly");
		sys3=baseSysParamsService.selectRefined(sys3);
		
		String value1=null;
		String value2=null;
		String value3=null;
		if(sys1!=null){
			value1=sys1.getValueParam();
		}
		if(sys2!=null){
			value2=sys2.getValueParam();
		}
		if(sys3!=null){
			value3=sys3.getValueParam();
		}
		List<String> listall1= new ArrayList<String>();
		List<String> listall2= new ArrayList<String>();
		List<String> listall3= new ArrayList<String>();
		List<DhOverdueLetterLawyers> list1=dhOverdueLetterLawyersService.selectCountByLawyers(value1);
		if(list1!=null && list1.size()>0){
			for (DhOverdueLetterLawyers dhOverdueLetterLawyers : list1) {
				listall1.add(dhOverdueLetterLawyers.getBusiId());
			}
		}
		List<DhOverdueCollectionPhone> list2=dhOverdueCollectionPhoneService.selectCountByPhone(value2);
		if(list2!=null && list2.size()>0){
			for (DhOverdueCollectionPhone dhOverdueCollectionPhone : list2) {
				listall2.add(dhOverdueCollectionPhone.getBusiId());
			}
		}
		YwBusinessLoanExt ywBusinessLoanExt= new YwBusinessLoanExt();
		ywBusinessLoanExt.withOutPage();
		ywBusinessLoanExt.setBaseWhere();
		ywBusinessLoanExt.setAmountMonthlyLess(Double.parseDouble(value3==null?"0":value3));
		List<YwBusinessLoan> list3=ywBusinessLoanService.selectByExt(ywBusinessLoanExt);
		if(list3!=null && list3.size()>0){
			for (YwBusinessLoan ywBusinessLoan : list3) {
				listall3.add(ywBusinessLoan.getBusiId());
			}
		}
		listall1.retainAll(listall2);
		listall1.retainAll(listall3);
		DhOverdueTrailer overdueTrailer=null;
		List<DhOverdueTrailer> list = new ArrayList<DhOverdueTrailer>();
		YwBusiness ywbusiness=null;
		if(listall1!=null && listall1.size()>0){
			for (String sstr : listall1) {
				overdueTrailer=new DhOverdueTrailer();
				overdueTrailer.setBusiId(sstr);
				overdueTrailer.setStatusTrailer(10);
				DhOverdueTrailer busiTrailer=dhOverdueTrailerService.selectRefined(overdueTrailer);
				if(busiTrailer!=null) continue;
				ywbusiness=new YwBusiness();
		    	ywbusiness=ywBusinessService.selectById(sstr);
		    	if(ywbusiness==null) continue;
		    	overdueTrailer.setId(UUIDFactory.getUUID());
	    		overdueTrailer.setLoanAmount(ywbusiness.getSignLoanAmount());
				overdueTrailer.setTypeTrailer(TypeTrailer.YU_QI.getCode());
				overdueTrailer.setStatusTrailer(10);
				overdueTrailer.setDpStatus(DpStatus.NORMAL.getCode());
				overdueTrailer.setTypeData(TypeData.SYSTEM.getCode());
				overdueTrailer.setCusId(ywbusiness.getCustomerId());
				overdueTrailer.setCusName(ywbusiness.getCustomerName());
				overdueTrailer.setCreated((int)(System.currentTimeMillis() / 1000));
	    		BaseSalersman baseSalersman=baseSalersmanService.selectById(ywbusiness.getSalesmanId());
	    		if(baseSalersman!=null){
	    			BaseBusiUnit baseBusiUnit=baseBusiUnitService.selectById(baseSalersman.getUnitId());
	    			if(baseBusiUnit!=null){
	    				overdueTrailer.setSalesmanId(ywbusiness.getSalesmanId());
	    				overdueTrailer.setSalesmanCode(ywbusiness.getSalesmanCode());
	    				overdueTrailer.setSalesmanName(baseSalersman.getName());
	    				overdueTrailer.setCustomerNo(ywbusiness.getCustomerNo());
	    				overdueTrailer.setUnitId(baseBusiUnit.getId());
	    				overdueTrailer.setUnitCode(baseBusiUnit.getOrgCode());
	    				overdueTrailer.setUnitName(baseBusiUnit.getOrgName());
	    				overdueTrailer.setBankLoan(ywbusiness.getBankLoan());
	    			}
	    		}
				list.add(overdueTrailer);
				busiTrailer=null;
			}
			if(list!=null && list.size()>0){
				dhOverdueTrailerService.insertBatch(list);
			}
		}
	}


	/**
	 * 抵押
	 */
	private void diYaList(){
		logger.debug("diYaList task start.");
		BaseSysParams baseSysParams= new BaseSysParams();
		baseSysParams.setCodeParam("Overdue_Mortgage");
		BaseSysParams sysParams=baseSysParamsService.selectRefined(baseSysParams);
		if(sysParams!=null){
			String valueParam=sysParams.getValueParam();
			DzMortgageExt dzMortgageExt= new DzMortgageExt();
			dzMortgageExt.setDateMortgageLess(Integer.parseInt(valueParam));
			dzMortgageExt.setBaseWhere();
			dzMortgageExt.withOutPage();
			List<DzMortgage> mortgage=dzmortgageService.selectByExt(dzMortgageExt);
			List<DhOverdueTrailer> list = new ArrayList<DhOverdueTrailer>();
			DhOverdueTrailer overdueTrailer=null;
			DhOverdueTrailer busiTrailer=null;
			YwBusiness ywbusiness=null;
			for (DzMortgage dzMortgage : mortgage) {
				overdueTrailer= new DhOverdueTrailer();
				busiTrailer= new DhOverdueTrailer();
				busiTrailer.setBusiId(dzMortgage.getBusiId());
				busiTrailer.setStatusTrailer(10);
				busiTrailer=dhOverdueTrailerService.selectRefined(busiTrailer);
				if(busiTrailer!=null) continue;
				overdueTrailer.setId(UUIDFactory.getUUID());
				overdueTrailer.setBusiId(dzMortgage.getBusiId());
				overdueTrailer.setCusId(dzMortgage.getCusId());
				overdueTrailer.setTypeTrailer(TypeTrailer.DI_YA.getCode());
				overdueTrailer.setStatusTrailer(10);
				overdueTrailer.setDpStatus(DpStatus.NORMAL.getCode());
				overdueTrailer.setTypeData(TypeData.SYSTEM.getCode());
				overdueTrailer.setCreated((int)(System.currentTimeMillis() / 1000));
				ywbusiness=ywBusinessService.selectById(dzMortgage.getBusiId());
		    	if(ywbusiness!=null){
		    		overdueTrailer.setSalesmanId(ywbusiness.getSalesmanId());
		    		overdueTrailer.setSalesmanCode(ywbusiness.getSalesmanCode());
		    		overdueTrailer.setLoanAmount(ywbusiness.getSignLoanAmount());
		    		overdueTrailer.setCusId(ywbusiness.getCustomerId());
		    		overdueTrailer.setCusName(ywbusiness.getCustomerName());
		    		overdueTrailer.setCustomerNo(ywbusiness.getCustomerNo());
		    		overdueTrailer.setBankLoan(ywbusiness.getBankLoan());
		    		BaseSalersman baseSalersman=baseSalersmanService.selectById(ywbusiness.getSalesmanId());
		    		if(baseSalersman!=null){
		    			BaseBusiUnit baseBusiUnit=baseBusiUnitService.selectById(baseSalersman.getUnitId());
		    			if(baseBusiUnit!=null){
		    				overdueTrailer.setSalesmanName(baseSalersman.getName());
		    				overdueTrailer.setUnitId(baseBusiUnit.getId());
		    				overdueTrailer.setUnitCode(baseBusiUnit.getOrgCode());
		    				overdueTrailer.setUnitName(baseBusiUnit.getOrgName());
		    			}
		    		}
		    	}
				list.add(overdueTrailer);
			}
			if(list!=null && list.size()>0){
				dhOverdueTrailerService.insertBatch(list);
			}
		};
		
	}
}
